// index.js
Page({
  data: {
    searchValue: '',
    currentCategory: 'all',
    recentTools: [],
    categoryNames: {
      all: '全部工具',
      daily: '日常工具',
      life: '生活实用',
      dev: '开发工具',
      image: '图片处理',
      office: '办公工具',
      fun: '娱乐工具'
    },
    allTools: [
      // 日常工具
      {
        id: 'qrcode',
        name: '二维码',
        desc: '生成和扫描二维码',
        category: 'daily',
        icon: '/assets/icons/qrcode.svg',
        path: '/pages/tools/qrcode/qrcode',
        developed: false
      },
      {
        id: 'calculator',
        name: '计算器',
        desc: '科学计算器',
        category: 'daily',
        icon: '/assets/icons/calculator.svg',
        path: '/pages/tools/calculator/calculator',
        developed: true
      },
      {
        id: 'converter',
        name: '单位转换',
        desc: '长度重量温度转换',
        category: 'daily',
        icon: '/assets/icons/converter.svg',
        path: '/pages/tools/converter/converter',
        developed: true
      },
      {
        id: 'color',
        name: '颜色工具',
        desc: 'RGB HEX颜色转换',
        category: 'daily',
        icon: '/assets/icons/color.svg',
        path: '/pages/tools/color/color',
        developed: true
      },
      {
        id: 'text',
        name: '文本工具',
        desc: '字数统计格式转换',
        category: 'daily',
        icon: '/assets/icons/text.svg',
        path: '/pages/tools/text/text',
        developed: true
      },
      {
        id: 'timer',
        name: '时间工具',
        desc: '倒计时秒表时区',
        category: 'daily',
        icon: '/assets/icons/timer.svg',
        path: '/pages/tools/timer/timer',
        developed: false
      },
      // 生活实用
      {
        id: 'weather',
        name: '天气查询',
        desc: '实时天气预报',
        category: 'life',
        icon: '/assets/icons/weather.svg',
        path: '/pages/tools/weather/weather',
        developed: false
      },
      {
        id: 'express',
        name: '快递查询',
        desc: '物流信息追踪',
        category: 'life',
        icon: '/assets/icons/express.svg',
        path: '/pages/tools/express/express',
        developed: false
      },
      {
        id: 'exchange',
        name: '汇率换算',
        desc: '实时汇率查询',
        category: 'life',
        icon: '/assets/icons/exchange.svg',
        path: '/pages/tools/exchange/exchange',
        developed: false
      },
      {
        id: 'idcard',
        name: '身份证查询',
        desc: '归属地生日查询',
        category: 'life',
        icon: '/assets/icons/idcard.svg',
        path: '/pages/tools/idcard/idcard',
        developed: false
      },
      {
        id: 'phone',
        name: '手机归属地',
        desc: '运营商地区查询',
        category: 'life',
        icon: '/assets/icons/phone.svg',
        path: '/pages/tools/phone/phone',
        developed: false
      },
      {
        id: 'ip',
        name: 'IP查询',
        desc: 'IP归属地查询',
        category: 'life',
        icon: '/assets/icons/ip.svg',
        path: '/pages/tools/ip/ip',
        developed: false
      },
      // 开发工具
      {
        id: 'json',
        name: 'JSON工具',
        desc: '格式化压缩验证',
        category: 'dev',
        icon: '/assets/icons/json.svg',
        path: '/pages/tools/json/json',
        developed: false
      },
      {
        id: 'base64',
        name: 'Base64',
        desc: '编码解码转换',
        category: 'dev',
        icon: '/assets/icons/base64.svg',
        path: '/pages/tools/base64/base64',
        developed: false
      },
      {
        id: 'url',
        name: 'URL编码',
        desc: 'URL参数编解码',
        category: 'dev',
        icon: '/assets/icons/url.svg',
        path: '/pages/tools/url/url',
        developed: false
      },
      {
        id: 'hash',
        name: '哈希加密',
        desc: 'MD5 SHA加密',
        category: 'dev',
        icon: '/assets/icons/hash.svg',
        path: '/pages/tools/hash/hash',
        developed: false
      },
      {
        id: 'regex',
        name: '正则测试',
        desc: '正则表达式测试',
        category: 'dev',
        icon: '/assets/icons/regex.svg',
        path: '/pages/tools/regex/regex',
        developed: false
      },
      {
        id: 'timestamp',
        name: '时间戳',
        desc: 'Unix时间戳转换',
        category: 'dev',
        icon: '/assets/icons/timestamp.svg',
        path: '/pages/tools/timestamp/timestamp',
        developed: false
      },
      // 图片处理
      {
        id: 'compress',
        name: '图片压缩',
        desc: '无损有损压缩',
        category: 'image',
        icon: '/assets/icons/compress.svg',
        path: '/pages/tools/compress/compress',
        developed: false
      },
      {
        id: 'format',
        name: '格式转换',
        desc: 'JPG PNG WEBP转换',
        category: 'image',
        icon: '/assets/icons/format.svg',
        path: '/pages/tools/format/format',
        developed: false
      },
      {
        id: 'resize',
        name: '尺寸调整',
        desc: '裁剪缩放旋转',
        category: 'image',
        icon: '/assets/icons/resize.svg',
        path: '/pages/tools/resize/resize',
        developed: false
      },
      {
        id: 'filter',
        name: '图片滤镜',
        desc: '美化特效处理',
        category: 'image',
        icon: '/assets/icons/filter.svg',
        path: '/pages/tools/filter/filter',
        developed: false
      },
      // 办公工具
      {
        id: 'pdf',
        name: 'PDF工具',
        desc: '合并分割压缩',
        category: 'office',
        icon: '/assets/icons/pdf.svg',
        path: '/pages/tools/pdf/pdf',
        developed: false
      },
      {
        id: 'excel',
        name: 'Excel工具',
        desc: 'CSV转换处理',
        category: 'office',
        icon: '/assets/icons/excel.svg',
        path: '/pages/tools/excel/excel',
        developed: false
      },
      {
        id: 'word',
        name: 'Word工具',
        desc: '文档格式转换',
        category: 'office',
        icon: '/assets/icons/word.svg',
        path: '/pages/tools/word/word',
        developed: false
      },
      {
        id: 'markdown',
        name: 'Markdown',
        desc: 'MD预览转换',
        category: 'office',
        icon: '/assets/icons/markdown.svg',
        path: '/pages/tools/markdown/markdown',
        developed: false
      },
      {
        id: 'invoice',
        name: '发票助手',
        desc: '发票信息提取',
        category: 'office',
        icon: '/assets/icons/invoice.svg',
        path: '/pages/tools/invoice/invoice',
        developed: false
      },
      // 娱乐工具
      {
        id: 'lottery',
        name: '随机抽奖',
        desc: '抽奖转盘随机选择',
        category: 'fun',
        icon: '/assets/icons/lottery.svg',
        path: '/pages/tools/lottery/lottery',
        developed: false
      },
      {
        id: 'dice',
        name: '骰子工具',
        desc: '多面骰子随机数',
        category: 'fun',
        icon: '/assets/icons/dice.svg',
        path: '/pages/tools/dice/dice',
        developed: false
      },
      {
        id: 'password',
        name: '密码生成',
        desc: '安全密码随机生成',
        category: 'fun',
        icon: '/assets/icons/password.svg',
        path: '/pages/tools/password/password',
        developed: false
      },
      {
        id: 'music',
        name: '音乐工具',
        desc: '音频格式转换',
        category: 'fun',
        icon: '/assets/icons/music.svg',
        path: '/pages/tools/music/music',
        developed: false
      },
      {
        id: 'game',
        name: '小游戏',
        desc: '休闲益智游戏',
        category: 'fun',
        icon: '/assets/icons/game.svg',
        path: '/pages/tools/game/game',
        developed: false
      }
    ],
    filteredTools: []
  },

  onLoad() {
    this.setData({
      filteredTools: this.data.allTools
    });
    this.loadRecentTools();
  },

  onShow() {
    this.loadRecentTools();
  },

  // 搜索输入
  onSearchInput(e) {
    const value = e.detail.value;
    this.setData({
      searchValue: value
    });
    this.filterTools();
  },

  // 切换分类
  switchCategory(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      currentCategory: category,
      searchValue: ''
    });
    this.filterTools();
  },

  // 过滤工具
  filterTools() {
    let tools = this.data.allTools;
    
    // 按分类过滤
    if (this.data.currentCategory !== 'all') {
      tools = tools.filter(tool => tool.category === this.data.currentCategory);
    }
    
    // 按搜索关键词过滤
    if (this.data.searchValue) {
      const keyword = this.data.searchValue.toLowerCase();
      tools = tools.filter(tool => 
        tool.name.toLowerCase().includes(keyword) ||
        tool.desc.toLowerCase().includes(keyword)
      );
    }
    
    this.setData({
      filteredTools: tools
    });
  },

  // 打开工具
  openTool(e) {
    const tool = e.currentTarget.dataset.tool;
    if (tool && tool.path) {
      // 检查工具是否已开发
      if (!tool.developed) {
        wx.showToast({
          title: '该工具正在开发中',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      
      // 添加到最近使用
      this.addToRecent(tool);
      
      // 跳转到工具页面
      wx.navigateTo({
        url: tool.path
      });
    }
  },

  // 添加到最近使用
  addToRecent(tool) {
    let recent = wx.getStorageSync('recentTools') || [];
    
    // 移除已存在的
    recent = recent.filter(item => item.id !== tool.id);
    
    // 添加到开头
    recent.unshift(tool);
    
    // 最多保存6个
    if (recent.length > 6) {
      recent = recent.slice(0, 6);
    }
    
    wx.setStorageSync('recentTools', recent);
    this.setData({
      recentTools: recent
    });
  },

  // 加载最近使用
  loadRecentTools() {
    const recent = wx.getStorageSync('recentTools') || [];
    this.setData({
      recentTools: recent
    });
  }
})
